package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.exception.PlatformBizErrorCodes;
import com.iwomy.secureplat.common.core.util.MsgUtils;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.dto.BasicDeviceTypeDTO;
import com.iwomy.secureplat.platform.entity.BasicDeviceModelEntity;
import com.iwomy.secureplat.platform.entity.BasicDeviceTypeEntity;
import com.iwomy.secureplat.platform.exception.PlatformException;
import com.iwomy.secureplat.platform.service.BasicDeviceModelService;
import com.iwomy.secureplat.platform.service.BasicDeviceTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备类型
 *
 * @author pig
 * @date 2023-11-14 10:07:32
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicDeviceType")
@Tag(description = "basicDeviceType", name = "设备类型管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicDeviceTypeController {

	private final BasicDeviceTypeService basicDeviceTypeService;

	private final BasicDeviceModelService basicDeviceModelService;

	/**
	 * 分页查询
	 *
	 * @param page               分页对象
	 * @param basicDeviceTypeDTO 设备类型
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_basicDeviceType_view')")
	public R getBasicDeviceTypePage(@ParameterObject Page page, @ParameterObject BasicDeviceTypeDTO basicDeviceTypeDTO) {
		return R.ok(basicDeviceTypeService.getBasicDeviceTypePage(page, basicDeviceTypeDTO));
	}

	/**
	 * 分页查询
	 *
	 * @param basicDeviceType 设备类型
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/list")
	public R getBasicDeviceTypeList(@ParameterObject BasicDeviceTypeEntity basicDeviceType) {
		return R.ok(basicDeviceTypeService.pageByScope(basicDeviceType));
	}


	/**
	 * 通过id查询设备类型
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_basicDeviceType_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(basicDeviceTypeService.getById(id));
	}

	/**
	 * 新增设备类型
	 *
	 * @param basicDeviceType 设备类型
	 * @return R
	 */
	@Operation(summary = "新增设备类型", description = "新增设备类型")
	@SysLog("新增设备类型")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_basicDeviceType_add')")
	public R save(@RequestBody BasicDeviceTypeEntity basicDeviceType) {
		BasicDeviceTypeEntity wrapperEntity = new BasicDeviceTypeEntity();
		wrapperEntity.setTypeName(basicDeviceType.getTypeName());
		LambdaQueryWrapper<BasicDeviceTypeEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicDeviceTypeEntity one = basicDeviceTypeService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one)) {
//			throw new PlatformException("设备类型已存在");
			throw new PlatformException(MsgUtils.getMessage(PlatformBizErrorCodes.DEVICETYPE_ID_EXIST));
		}
		basicDeviceType.setDeptId(SecurityUtils.getUser().getDeptId());
		return R.ok(basicDeviceTypeService.save(basicDeviceType));
	}

	/**
	 * 修改设备类型
	 *
	 * @param basicDeviceType 设备类型
	 * @return R
	 */
	@Operation(summary = "修改设备类型", description = "修改设备类型")
	@SysLog("修改设备类型")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_basicDeviceType_edit')")
	public R updateById(@RequestBody BasicDeviceTypeEntity basicDeviceType) {
		long count = basicDeviceModelService
				.count(Wrappers.lambdaQuery(new BasicDeviceModelEntity()).eq(BasicDeviceModelEntity::getDeviceTypeId, basicDeviceType.getId()));

		if (count > 0)
//			return R.failed("设备类型已被使用不允许修改");
			return R.failed(MsgUtils.getMessage(PlatformBizErrorCodes.DEVICETYPE_ID_USED_MODIFY));
		BasicDeviceTypeEntity wrapperEntity = new BasicDeviceTypeEntity();
		wrapperEntity.setTypeName(basicDeviceType.getTypeName());
		LambdaQueryWrapper<BasicDeviceTypeEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicDeviceTypeEntity one = basicDeviceTypeService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one) && !one.getId().equals(basicDeviceType.getId())) {
//			throw new PlatformException("设备类型已存在");
			throw new PlatformException(MsgUtils.getMessage(PlatformBizErrorCodes.DEVICETYPE_ID_EXIST));
		}
		return R.ok(basicDeviceTypeService.updateById(basicDeviceType));
	}

	/**
	 * 启用禁用
	 *
	 * @param basicDeviceType 设备类型
	 * @return R
	 */
	@Operation(summary = "启用禁用设备类型", description = "启用禁用设备类型")
	@SysLog(expression = "'启用禁用设备类型'.concat(#basicDeviceType.remarks)")
	@PutMapping("/enableOrDisable")
	@PreAuthorize("@pms.hasPermission('platform_basicDeviceType_enableOrDisable')")
	public R enableOrDisable(@RequestBody BasicDeviceTypeDTO basicDeviceType) {

		BasicDeviceTypeEntity basicDeviceTypeEntity = new BasicDeviceTypeEntity();
		basicDeviceTypeEntity.setId(basicDeviceType.getId());
		basicDeviceTypeEntity.setRemarks(basicDeviceType.getRemarks());
		basicDeviceTypeEntity.setState(basicDeviceType.getState());
		return R.ok(basicDeviceTypeService.updateById(basicDeviceTypeEntity));
	}


	/**
	 * 通过id删除设备类型
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除设备类型", description = "通过id删除设备类型")
	@SysLog("通过id删除设备类型")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_basicDeviceType_del')")
	public R removeById(@RequestBody Long[] ids) {
		long count = basicDeviceModelService.count(Wrappers.<BasicDeviceModelEntity>lambdaQuery().in(BasicDeviceModelEntity::getDeviceTypeId, ids));

		if (count > 0)
//			return R.failed("设备类型已被使用不允许删除");
			return R.failed(MsgUtils.getMessage(PlatformBizErrorCodes.DEVICETYPE_ID_USED_DELETE));
		return R.ok(basicDeviceTypeService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param basicDeviceType 查询条件
	 * @param ids             导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_basicDeviceType_export')")
	public List<BasicDeviceTypeEntity> export(BasicDeviceTypeEntity basicDeviceType, Long[] ids) {
		return basicDeviceTypeService.list(Wrappers.lambdaQuery(basicDeviceType).in(ArrayUtil.isNotEmpty(ids), BasicDeviceTypeEntity::getId, ids));
	}
}